package com.kiln.juxingqs.fragment

import android.view.View
import android.widget.Space
import androidx.lifecycle.Observer
import client.global.commlib.base.BaseFragment
import client.global.commlib.comm_ui.draw.SpaceItemDecoration
import client.global.commlib.utils.Util
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.PtzxAdapter
import com.kiln.juxingqs.api.bean.ArticleInfo
import com.kiln.juxingqs.databinding.FragmentTabthreeBinding
import com.kiln.juxingqs.ui.vm.MainViewModel

class TabThreeFragment : BaseFragment<FragmentTabthreeBinding, MainViewModel>() {

    override fun getViewId(): Int {
        return R.layout.fragment_tabthree
    }

    override fun initViewModel(): MainViewModel = createViewModel()
    val adapter: PtzxAdapter by lazy {
        PtzxAdapter(requireContext())
    }

    override fun initWidget() {
        binding.vm = viewModel
        binding.recyclerView.adapter = adapter
        binding.emptyLayout.setOnClickListener {
            viewModel.zxInfo()
        }

        binding.recyclerView.addItemDecoration(SpaceItemDecoration(1))
        binding.statusBar.post {
            val h = Util.getStatusBarHeight(requireContext())
            val p = binding.statusBar.layoutParams
            p.height = h
            binding.statusBar.layoutParams = p
            Util.setBg(resources.getColor(R.color.state_bg), requireActivity().window)
        }
        viewModel.zxCall.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
                    adapter.hasMoreState.value = false
                    val list = it.data!!.article_list as MutableList<ArticleInfo.Article?>
                    if (list.size > 0) {
//                        list.add(
//                            ArticleInfo.Article(
//                                -1,
//                                "",
//                                "",
//                                "2020-01-25",
//                                1,
//                                "交易规则协议"
//                            )
//                        )
                        adapter.submitData(list)
                        adapter.hasMoreState.value = false
                    }
                    binding.emptyLayout.visibility = View.GONE
                } else {
                    if (adapter.itemCount == 0) {
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                    }
                }
            }

        })


    }


    override fun onResume() {
        super.onResume()
        viewModel.zxInfo()
    }
}